import { FileGenerateOption } from "../../model/FileGenerateOption";

import * as vscode from 'vscode';
import { MysqlConnector } from "../connect/MysqlConnector";
import { OracleConnector } from "../connect/OracleConnector";
import { EntityFileGenerate } from "./EntityFileGenerate";
import { MapperFileGenerate } from "./MapperFileGenerate";
import { XmlFileGenerate } from "./XmlFileGenerate";
import { TcpDBConnector } from "../connect/DBConnector";
import { ServiceFileGenerate } from "./ServiceFileGenerate";
import { ServiceImplFileGenerate } from "./ServiceImplFileGenerate";

export class FileGenerateFactory {
    type: string;
    options: FileGenerateOption;

    constructor(type: string, options: FileGenerateOption) {
        this.type = type;
        this.options = options;
    }

    /**
     * 文件生成
     */
    async generate(): Promise<void> {
        // 1. 获取数据库配置
        let dbEnvs = vscode.workspace.getConfiguration("mybatis-tools.connections").get<Array<any>>(this.type) || [];
        let selectedEnv = {
            tag: '',
            host: '',
            port: -1,
            user: '',
            password: '',
            database: '',
            sid: ''
        };
        if (dbEnvs && dbEnvs.length > 0) {
            for (let env of dbEnvs) {
                if (env.tag === this.options.tag) {
                    selectedEnv = env;
                    break;
                }
            }
        }
        let connect = null;
        if (this.type === 'mysql') {
            connect = new MysqlConnector();
            connect.host = selectedEnv.host;
            connect.port = selectedEnv.port;
            connect.user = selectedEnv.user;
            connect.password = selectedEnv.password;
            connect.database = selectedEnv.database;
        } else if (this.type === 'oracle') {
            connect = new OracleConnector();
            connect.host = selectedEnv.host;
            connect.port = selectedEnv.port;
            connect.sid = selectedEnv.sid;
            connect.user = selectedEnv.user;
            connect.password = selectedEnv.password;
            connect.database = selectedEnv.database;
        } else {
            throw new Error('不支持的数据库类型');
        }

        if (this.options.tableName) {
            this.generateTable(connect, this.options.tableName);
        } else {
            var tables = await connect.listTables(this.options);
            for (let t of tables) {
                await this.generateTable(connect, t.tableName);
            }
        }
    }

    async generateTable(connect: TcpDBConnector, tableName: string) {
        let columnInfos = await connect.listColumn(this.options, tableName);

        console.log('字段信息', columnInfos);
        if (!columnInfos || columnInfos.length === 0) {
            throw new Error("表字段不存在");
        }

        // 3. entity mapper xml 生成

        if (this.options.isEntityFile) {
            let entityMaker = new EntityFileGenerate(this.options, tableName, columnInfos);
            entityMaker.generate();
        }
        if (this.options.isInterfaceFile) {
            let interfaceMaker = new MapperFileGenerate(this.options, tableName, columnInfos);
            interfaceMaker.generate();
        }
        if (this.options.isServiceFile) {
            let serviceMaker = new ServiceFileGenerate(this.options, tableName, columnInfos);
            serviceMaker.generate();
        }
        if (this.options.isServiceImplFile) {
            let serviceImplMaker = new ServiceImplFileGenerate(this.options, tableName, columnInfos);
            serviceImplMaker.generate();
        }
        if (this.options.isXmlFile) {
            let xmlMaker = new XmlFileGenerate(this.options, tableName, columnInfos);
            xmlMaker.generate();
        }

    }

}
